#!/bin/bash

#    / **  
#    * 文件名：安装linux软件
#    * 版权： Copyright 谢振斌  All Rights Reserved.  
#    * 修改内容：新增  
#    * 修改人： 谢振斌  
#    * 修改时间：2021-3-23  
#    */  

#    / **  
#    *〈一句话功能简述〉  
#    *〈功能详细描述〉  
#    * @param [参数1] [参数1说明]  
#    * @param [参数2] [参数2说明]  
#    * @return [返回类型说明]  
#    * @exception/throws [违例类型] [违例说明]  
#    * @see [类、类#方法、类#成员]  
#    * @deprecated  
#    */  
function fun1()
{  
	echo "helld world !"

	# 在shell中：
	# $0: 获取当前脚本的名称
	# $#: 传递给脚本的参数个数
	# $$: shell脚本的进程号
	# $1, $2, $3...：脚本程序的参数
}


#    / **  
#    *〈获得发行版本名称〉  
#    *〈功能详细描述〉  
#    * @param [参数1] [参数1说明]  
#    * @param [参数2] [参数2说明]  
#    * @return [返回类型说明]  
#    * @exception/throws [违例类型] [违例说明]  
#    * @see [类、类#方法、类#成员]  
#    * @deprecated  
#    */  
function getDistName()
{
	if grep -Eqii "Arch Linux" /etc/issue || grep -Eq "Arch Linux" /etc/*-release; then
        DISTRO='Arch Linux'
        PM='pacman'
    elif grep -Eqii "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
        DISTRO='CentOS'
        PM='yum'
    elif grep -Eqi "Red Hat Enterprise Linux Server" /etc/issue || grep -Eq "Red Hat Enterprise Linux Server" /etc/*-release; then
        DISTRO='RHEL'
        PM='yum'
    elif grep -Eqi "Aliyun" /etc/issue || grep -Eq "Aliyun" /etc/*-release; then
        DISTRO='Aliyun'
        PM='yum'
    elif grep -Eqi "Fedora" /etc/issue || grep -Eq "Fedora" /etc/*-release; then
        DISTRO='Fedora'
        PM='yum'
    elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
        DISTRO='Debian'
        PM='apt'
    elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
        DISTRO='Ubuntu'
        PM='apt'
    elif grep -Eqi "Raspbian" /etc/issue || grep -Eq "Raspbian" /etc/*-release; then
        DISTRO='Raspbian'
        PM='apt'
    else
        DISTRO='unknow'
    fi

    # 返回值
    echo $DISTRO;
}

#    / **  
#    *〈 安装软件 〉  
#    *〈功能详细描述〉  
#    * @param [参数1] [软件名称]  
#    * @param [参数2] [参数2说明]  
#    * @return [返回类型说明]  
#    * @exception/throws [违例类型] [违例说明]  
#    * @see [类、类#方法、类#成员]  
#    * @deprecated  
#    */  
function installSoftware()
{
    echo "[INFO] 安装软件"
    softwareName=$1
    echo "[Debug] softwareName="${softwareName}
    # 判断是否已经安装
    cmdStr="which "${softwareName}
    if [ -x "$(cmdStr)" ]; then
        # 获取当前系统发行版本
        systemType=$(getDistName)
        echo "[Debug] systemType="${systemType}
        # 命令拼接
        #cmdStr="ln -s  "${dir}"/zshrc-oh-my-sh  "${HOME}"/.zshrc"
        #cmdStr="git clone "${URL}${pluginsName}" "${pluginsPath}
        #echo "执行命令: ${cmdStr}"
        #${cmdStr}
        if [ "$systemType" == "CentOS" ]; then
            echo "[Debug] ======= CentOS"
            # 执行另一个脚本
            # /bin/bash ./zsh1.sh  
            #cmdStr="ln -s  "${dir}"/zshrc-oh-my-sh  "${HOME}"/.zshrc"
            cmdStr="yum install "${softwareName}
            echo "执行命令: ${cmdStr}"
            ${cmdStr}
        elif [ "$systemType" == "Arch Linux" ] ; then
            echo "[Debug] ======= Arch Linux"
            cmdStr="yay -S "${softwareName}
            echo "执行命令: ${cmdStr}"
            ${cmdStr}
        else
            echo "[Debug] ======= unknow"
            echo "[INFO] 不知道当前系统是什么，不知到该用什么包管理软件进行安装！软件${systemType}"
        fi
      if [ ! $? -eq 0 ]; then
        echo ""
        echo "[ERROR]: 安装软件${softwareName}  失败 !!"
        exit
      fi
    else
        echo "[INFO] 软件${softwareName}已经存在！"
    fi
}

#    / **  
#    *〈 安装软件 从git仓库〉  
#    *〈功能详细描述〉  
#    * @param [参数1] [git仓库地址]  
#    * @param [参数2] [本地存放地址]  
#    * @param [参数3] [克隆后执行的安装命令] 
#    * @return [返回类型说明]  
#    * @exception/throws [违例类型] [违例说明]  
#    * @see [类、类#方法、类#成员]  
#    * @deprecated  
#    */  
function installSoftwareFormGit()
{
    echo "[INFO] 开始 从git仓库安装软件 "
    pluginsName=$1
    echo "[Debug] pluginsName="${pluginsName}
    pluginsPath=$2
    echo "[Debug] pluginsPath="${pluginsPath}
    echo "[INFO] 安装插件："${pluginsName}
    executableInstallCmdStr=$3
    echo "[Debug] executableInstallCmdStr="${executableInstallCmdStr}

    # 判断是否路径是否已经存在
    if [ ! -d "$pluginsPath" ]; then
      echo "Installing "${pluginsName}
      #URL="https://github.com/"
      #URL="https://hub.fastgit.org/"
      URL="https://ghproxy.com/https://github.com/"
      if [ -x "$(which git)" ]; then
        # 命令拼接
        #cmdStr="ln -s  "${dir}"/zshrc-oh-my-sh  "${HOME}"/.zshrc"
        cmdStr="git clone "${URL}${pluginsName}" "${pluginsPath}
        echo "执行命令: ${cmdStr}"
        ${cmdStr}
        echo "[INFO] 安装插件"${pluginsName}"安装完成！"
        # 
        if [ ! -d "$pluginsPath" ]; then
            cmdStr="cd "${pluginsPath}
            echo "执行命令: ${cmdStr}"
            ${cmdStr}
            # git克隆完成，执行相关安装命令
            echo "git克隆完成，执行命令: ${executableInstallCmdStr}"
            ${cmdStr}
        fi
      else
        echo "[ERROR]: 请先安装 git !!"
        exit
      fi
      if [ ! $? -eq 0 ]; then
        echo ""
        echo "[ERROR]: 克隆${pluginsName}仓库中出错!!"
        exit
      fi
    else
        echo "[INFO] ${pluginsName}已经存在！"
    fi
}

#    / **  
#    *〈流程控制〉  
#    *〈该脚本的主函数〉  
#    * @param [无]  
#    * @return [无]  
#    * @exception/throws [无]  
#    * @deprecated  
#    */  
function processControlMain()  
{  
    # 打印当前系统时间  
    echo "Start..."  ;
    date;

    # 此处是你的代码  
    # ...  
    # fun1
	# 获取当前脚本目录位置
	cd `dirname $0`;
	dir=`pwd`;
	echo ${dir};

	mkdir -p ~/.tmux;
	# 备份 .tmux.conf 配置文件;
	mv ~/.tmux.conf ~/.tmux.conf-`date +%y%m%d%H%M%S`;
	# 建立软链接命令拼接;
	cmdStr="ln -s  "${dir}"/.tmux.conf  "${HOME}"/.tmux.conf";
	echo "执行命令: ${cmdStr} 建立软链接";
	${cmdStr};

    echo "";
    echo "";
    echo "";
    echo "=================================================================================";
    echo " 配置完成！请在终端执行命令： tmux 打开tmux，进行插件安装";
    echo "";
    echo " 进入打开tmux 后，按 prefix + I （ 前缀键(Ctrl+b) + 大写的 i ） 进行安装其他插件";
    echo "";
    echo " 其他：";
    echo " 前缀键 + U         ： 更新插件";
    echo " 前缀键 + Alt + u   ： 更新插件与移除（不在列表中的插件）";
    echo " 注:（安装过程是异步 会安装在 ~/.tmux/plugins 文件夹下"
    echo "================================================================================";
    echo "";
    echo "";
    echo "";
	## 备份 tmux-powerline 配置文件
	# mv ~/.tmux/tmux-powerline ~/.tmux/tmux-powerline-`date +%y%m%d%H%M%S`
	## 建立软链接命令拼接
	# cmdStr="ln -s  "${dir}"/tmux-powerline  "${HOME}"/.tmux/tmux-powerline"
	# echo "执行命令: ${cmdStr} 建立软链接"
	# ${cmdStr}


	## 备份 tmux-resurrect 配置文件
	#mv ~/.tmux/plugins/plugins/tmux-resurrect ~/.tmux/plugins/tmux-resurrect-`date +%y%m%d%H%M%S`
	## 建立软链接命令拼接
	#cmdStr="ln -s  "${dir}"/plugins/tmux-resurrect "${HOME}"/.tmux/plugins/tmux-resurrect"
	#echo "执行命令: ${cmdStr} 建立软链接"
	#${cmdStr}

	## 备份 tmux-continuum 配置文件
	# mv ~/.tmux/plugins/tmux-continuum ~/.tmux/plugins/tmux-continuum-`date +%y%m%d%H%M%S`
	## 建立软链接命令拼接
	# cmdStr="ln -s  "${dir}"/plugins/tmux-continuum  "${HOME}"/.tmux/plugins/tmux-continuum"
	# echo "执行命令: ${cmdStr} 建立软链接"
	# ${cmdStr}

	## 备份 tpm 配置文件
	# mv ~/.tmux/plugins/tpm ~/.tmux/plugins/tpm-`date +%y%m%d%H%M%S`
	## 建立软链接命令拼接
	# cmdStr="ln -s  "${dir}"/plugins/tpm  "${HOME}"/.tmux/plugins/tpm"
	# echo "执行命令: ${cmdStr} 建立软链接"
	# ${cmdStr}

	# 重载配置
	tmux source-file ~/.tmux.conf
    # 打印当前系统时间  
    date  
    echo "The End."
}  

# 执行入口 
# 进入脚本目录位置
cd `dirname $0`
# 获取当前脚本目录位置
shellfilepath=`pwd`'/'$0
# 打印脚本目录地址
echo "[INFO]: 脚本地址 "${shellfilepath}

if true  
# if false  
then  
    echo "[Debug] true"
    # 开始执行  从标准输入中读取并同时写入到标准输出和指定的文件上 
    ## 拼接日志文件地址
    #logfilepath=`pwd`'/'$0'.log'
    logfilepath=`pwd`'/'$0'-'$(date +%y%m%d-%H-%M-%S)'.log'
    ## 打印日志文件地址
    echo "[INFO]: 日志地址" ${logfilepath}
    processControlMain  | tee -a ${logfilepath}
else  
    echo "[Debug] false"  
    ## 函数单元测试  
    #  
fi  

# 脚本结束  
# 软件配置文件软链接设置脚本



